package com.maybach.core.manager.impl;

import com.maybach.util.query.support.PageResult;
import com.maybach.common.dto.UserInfoDTO;
import com.maybach.common.model.UserDO;
import com.maybach.common.query.UserQuery;
import com.maybach.common.query.ext.UserListQuery;
import com.maybach.core.dal.mapper.ext.UserExtMapper;
import com.maybach.core.form.user.UserUpdateStatusForm;
import com.maybach.core.manager.UserManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

/**
 * Manager for User.
 */

@Component
public class UserManagerImpl implements UserManager {

	protected Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected UserExtMapper userMapper;

	/**
	 * query count by query condition.
	 */
	@Override
	public int countByQuery(UserQuery query) {
		return userMapper.countByQuery(query);
	}

	/**
	 * delete by query condition.
	 */
	@Override
	public int deleteByQuery(UserQuery query) {
		return userMapper.deleteByQuery(query);
	}

	/**
	 * delete by primary key.
	 */
	@Override
	public int deleteByPrimaryKey(UserDO record) {
		return userMapper.deleteByPrimaryKey(record);
	}

	/**
	 * insert selective.
	 */
	@Override
	public long insertSelective(UserDO record) {
		return userMapper.insertSelective(record);
	}

	/**
	 * select by query condition.
	 */
	@Override
	public List<UserDO> selectByQuery(UserQuery query) {
		return userMapper.selectByQuery(query);
	}

	/**
	 * select by query condition with page.
	 */
	@Override
	public PageResult<UserDO> selectByQueryWithPage(UserQuery query) {
		PageResult<UserDO> result = new PageResult<UserDO>();
		result.setPageSize(query.getPageSize());
		result.setPageNo(query.getPageNo());
		result.setTotalCount(this.countByQuery(query));
		result.setResult(this.selectByQuery(query));
		return result;
	}

	/**
	 * select by primary key.
	 */
	@Override
	public UserDO selectByPrimaryKey(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	/**
	 * update by query condition selective.
	 */
	@Override
	public int updateByQuerySelective(UserDO record, UserQuery query) {
		return userMapper.updateByQuerySelective(record, query);
	}

	/**
	 * update by query condition.
	 */
	@Override
	public int updateByQuery(UserDO record, UserQuery query) {

		return userMapper.updateByQuery(record, query);
	}

	/**
	 * update by primary key selective.
	 */
	@Override
	public int updateByPrimaryKeySelective(UserDO record) {
		return userMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据查询条件查询用户列表
	 *
	 * @param query
	 * @return
	 */
	@Override
	public List<UserInfoDTO> queryUsersByQuery(UserListQuery query) throws SQLException {
		return userMapper.queryUsersByQueryDao(query);
	}

	/**
	 * 根据查询条件获取总个数
	 *
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	@Override
	public Integer countUsersByQuery(UserListQuery query) throws SQLException {
		return userMapper.countUsersByQueryDao(query);
	}

	/**
	 * @param form
	 * @return
	 * @throws SQLException
	 */
	@Override
	public int updateUserStatusByForm(UserUpdateStatusForm form) throws SQLException {
		return userMapper.updateUserStatusByFormDao(form);
	}

	/**
	 * 根据用户ID获取商家id
	 *
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	@Override
	public Long queryPartnerIdByUserIdDao(Long userId) throws SQLException {
		return userMapper.queryPartnerIdByUserIdDao(userId);
	}

	/**
	 * 查询是否存在用户
	 *
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	@Override
	public int countUserByUserId(Long userId) throws SQLException {
		return userMapper.countUserByUserId(userId);
	}

	@Override
	public UserDO queryByExtNo(String extNo) throws SQLException {
		return userMapper.selectByExtNo(extNo);
	}

}